package com.xxx.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@RestController
@RequestMapping("/load")
public class LoadController {

    @Value("${file.uploadPath}")
    private String filePath;

    @GetMapping("/download")
    public void download(String name, HttpServletResponse resp){


        resp.setContentType("image/jpeg");
        try (
                FileInputStream fos = new FileInputStream(filePath + name);
                ServletOutputStream outputStream = resp.getOutputStream()
        ) {
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fos.read(bytes)) != -1){
                outputStream.write(bytes,0,len);
                outputStream.flush();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
